package org.loofer.transferlive.fragment;

import android.app.AlertDialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import org.loofer.transferlive.R;
import org.loofer.transferlive.adapter.MessageAdapter;
import org.loofer.transferlive.view.PeriscopeLayout;
import org.loofer.transferlive.view.TimeTaskScroll;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

/**
 * Loofer
 * date:2016-9-19
 * */
public class SecondFragment extends Fragment implements OnClickListener{
	// 心型气泡
	private PeriscopeLayout periscopeLayout;
	private Handler handler=new Handler(){

		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			switch (msg.what) {
				case 1:
					periscopeLayout.addHeart();
					break;
				case 2:
					//在这里更新listview
					//adapter.setData(data);
					//adapter.notifyDataSetChanged();
					//lst_message.setAdapter(adapter);
					//lst_message.smoothScrollToPosition(index);
					break;
				default:
					break;
			}
		}
	};
	private ListView lst_message;
	List<String> data = new ArrayList<String>();
	private Timer timer = new Timer();
	private TimerTask task;
	private int index;
	private MessageAdapter adapter;
	private ImageView quit;

	@Override
	public View onCreateView(LayoutInflater inflater,
							 @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		View view=LayoutInflater.from(getActivity()).inflate(R.layout.second, null);
		//adapter=new MessageAdapter(getActivity());
		initView(view);
		return view;
	}

	private void initView(View view){
		// 初始化
		periscopeLayout = (PeriscopeLayout) view.findViewById(R.id.periscope);
		//调用添加泡泡的方法
		/**
		 * 在此处来调用使泡泡不断的产生，作者因为使用timertask在刷泡泡的时候出现了错误
		 * 所以选择使用下面的for循环来实现
		 * 虽然效果实现了，但是我还是不推荐这样使用，希望大牛有好的方法推荐，并优化修改下面的问题
		 * */
		new Thread(new Runnable() {

			@Override
			public void run() {
				// TODO Auto-generated method stub
				for (int i = 0; i <=Integer.MAX_VALUE; i++) {
					//periscopeLayout.addHeart();
					try {
						Thread.sleep(50);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					Message message = new Message();
					message.what = 1;
					handler.sendMessage(message);
					if(i==Integer.MAX_VALUE){
						i=0;
					}
				}
			}
		}).start();

		lst_message=(ListView) view.findViewById(R.id.lst_message);
		data=getData();
		new Timer().schedule(new TimeTaskScroll(getActivity(), lst_message,data), 20, 10);
		/**
		 * 以下方法是另一种更新并加载listview的方法，并且使listview不断滚动，此方法可以无限更新新数据
		 * 作者并未作验证，按照分析可行
		 * 而本demo的滚动是listview的数据源是写死的，无法加载更多，如果你想动态加载数据，请尝试我推荐的方法
		 * */
//		task = new TimerTask() {
//			@Override
//			public void run() {
//
//				data.add("This is a test message");
//	    	    data.add("created by Loofer");
//	    	    data.add("date 27/jun/2016");
//	    	    data.add("--------");
//				index+=2;
//
//				Message message = new Message();
//				message.what = 2;
//				handler.sendMessage(message);
//				}
//		};
//
//		timer.schedule(task, 1,40);

		quit=(ImageView) view.findViewById(R.id.quit);
		quit.setOnClickListener(this);
	}

	private List<String> getData(){
		List<String> data=new ArrayList<String>();
		for (int i = 0; i < 30; i++) {
			data.add("This is a test message");
			data.add("created by Loofer");
			data.add("date 19/Sept/2016");
			data.add("--------");
		}

		return data;
	}

	private void showExitDialog(){
		final AlertDialog dlg = new AlertDialog.Builder(getActivity()).create();
		dlg.show();
		Window window = dlg.getWindow();
		// *** 主要就是在这里实现这种效果的.
		// 设置窗口的内容页面,shrew_exit_dialog.xml文件中定义view内容
		window.setContentView(R.layout.alert_dialog);
		// 为确认按钮添加事件,执行退出应用操作
		TextView txt_dialog_title=(TextView) window.findViewById(R.id.txt_dialog_title);
		txt_dialog_title.setText("您确定要退出直播间吗？");
		TextView txt_dialog_cancel = (TextView) window
				.findViewById(R.id.txt_cancel);
		TextView txt_dialog_ok = (TextView) window
				.findViewById(R.id.txt_ok);

		txt_dialog_cancel.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				dlg.cancel();
			}
		});
		txt_dialog_ok.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				dlg.cancel();
				getActivity().finish();
			}
		});

		dlg.setCancelable(true);
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
			case R.id.quit:
				showExitDialog();
				break;

			default:
				break;
		}
	}
}
